﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Cocbook_DAL
{
    public class Book_AuthorDAL
    {
        public bool CreateBook_Author(Book_Author ba)
        {
            DB.SetQuery("INSERT INTO Book_Author VALUES(@aid,@bid)");
            DB.AddParameters("bid", ba.Book.BookID);
            DB.AddParameters("aid", ba.Author.AuthorID);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.CreateBook_Author", e.ToString());
                return false;
            }
        }

        public bool DeleteBook_Author(int bid, int aid)
        {
            DB.SetQuery("DELETE FROM Book_Author WHERE (BookID = @bid) and(AuthorID = @aid)");
            DB.AddParameters("bid", bid);
            DB.AddParameters("aid", aid);
            try
            {
                DB.ExeNoneQuery();
                return true;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.DeleteBook_Author", e.ToString());
                return false;
            }
        }

        public List<Book_Author> GetBook_AuthorByBookID(int id)
        {
            DB.SetQuery("SELECT BookID, AuthorID FROM Book_Author WHERE BookID = @id");
            DB.AddParameters("id", id);

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Book_Author> BA = new List<Book_Author>();
                Book_Author ba;
                foreach (List<object> lo in llo)
                {
                    ba = new Book_Author();
                    ba.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[0]));
                    ba.Author = (new AuthorDAL()).GetAuthorByID(Convert.ToInt16(lo[1]));
                    BA.Add(ba);
                }
                return BA;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetBook_AuthorByBookID", e.ToString());
                return null;
            }
        }

        public List<Book_Author> GetBook_AuthorByAuthorID(int id)
        {
            DB.SetQuery("SELECT BookID, AuthorID FROM Book_Author WHERE AuthorID = @id");
            DB.AddParameters("id", id);

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Book_Author> BA = new List<Book_Author>();
                Book_Author ba;
                foreach (List<object> lo in llo)
                {
                    ba = new Book_Author();
                    ba.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[0]));
                    ba.Author = (new AuthorDAL()).GetAuthorByID(Convert.ToInt16(lo[1]));
                    BA.Add(ba);
                }
                return BA;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetBook_AuthorByAuthorID", e.ToString());
                return null;
            }
        }

        public List<Book_Author> GetAllBook_Author()
        {
            DB.SetQuery("SELECT BookID, AuthorID FROM Book_Author");

            try
            {
                List<List<object>> llo = new List<List<object>>();
                llo = DB.ExeQuery();
                List<Book_Author> BA = new List<Book_Author>();
                Book_Author ba;
                foreach (List<object> lo in llo)
                {
                    ba = new Book_Author();
                    ba.Book = (new BookDAL()).GetBookById(Convert.ToInt16(lo[0]));
                    ba.Author = (new AuthorDAL()).GetAuthorByID(Convert.ToInt16(lo[1]));
                    BA.Add(ba);
                }
                return BA;
            }
            catch (Exception e)
            {
                Logger.Instance.Log("Program.Main.Test.GetAllBook_Author", e.ToString());
                return null;
            }
        }
    }
}
